<?php

use yii\db\Migration;

/**
 * Handles the creation of table `{{%data}}`.
 * Has foreign keys to the tables:
 *
 * - `{{%user_backend}}`
 * - `{{%file}}`
 * - `{{%file}}`
 */
class m200411_020034_create_data_table extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
    	$tableOptions = null;
		if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }
        $this->createTable('{{%data}}', [
            'id' => $this->primaryKey(),
            'user_id' => $this->integer()->notNull(),
            'model_id' => $this->integer()->notNull(),
            'image_id' => $this->integer()->notNull(),
            'created_at' => $this->dateTime(),
        ],  $tableOptions);

        // creates index for column `user_id`
        $this->createIndex(
            '{{%idx-data-user_id}}',
            '{{%data}}',
            'user_id'
        );

        // add foreign key for table `{{%user_backend}}`
        $this->addForeignKey(
            '{{%fk-data-user_id}}',
            '{{%data}}',
            'user_id',
            '{{%user_backend}}',
            'id',
            'CASCADE'
        );

        // creates index for column `model_id`
        $this->createIndex(
            '{{%idx-data-model_id}}',
            '{{%data}}',
            'model_id'
        );

        // add foreign key for table `{{%file}}`
        $this->addForeignKey(
            '{{%fk-data-model_id}}',
            '{{%data}}',
            'model_id',
            '{{%file}}',
            'id',
            'CASCADE'
        );

        // creates index for column `image_id`
        $this->createIndex(
            '{{%idx-data-image_id}}',
            '{{%data}}',
            'image_id'
        );

        // add foreign key for table `{{%file}}`
        $this->addForeignKey(
            '{{%fk-data-image_id}}',
            '{{%data}}',
            'image_id',
            '{{%file}}',
            'id',
            'CASCADE'
        );
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        // drops foreign key for table `{{%user_backend}}`
        $this->dropForeignKey(
            '{{%fk-data-user_id}}',
            '{{%data}}'
        );

        // drops index for column `user_id`
        $this->dropIndex(
            '{{%idx-data-user_id}}',
            '{{%data}}'
        );

        // drops foreign key for table `{{%file}}`
        $this->dropForeignKey(
            '{{%fk-data-model_id}}',
            '{{%data}}'
        );

        // drops index for column `model_id`
        $this->dropIndex(
            '{{%idx-data-model_id}}',
            '{{%data}}'
        );

        // drops foreign key for table `{{%file}}`
        $this->dropForeignKey(
            '{{%fk-data-image_id}}',
            '{{%data}}'
        );

        // drops index for column `image_id`
        $this->dropIndex(
            '{{%idx-data-image_id}}',
            '{{%data}}'
        );

        $this->dropTable('{{%data}}');
    }
}
